<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Submit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    $().ready(function () {
        $("#contests").change();
        $("#problemId").change();
    });

    $("#contests").live("change", function () {
        $("#contestProblems select").hide();
        var id = $("#contests").val();
        $("select[name=" + id + "]").show();
    });

    function updateIdBox( id ) {
        $("#problemId").val($("select[name=" + id + "]").val());
    }

    $("#problemId").live("change", function () {        
        if ($("#contestProblems select").val($("#problemId").val()).find("option:selected").text() == "") {
            $("#contestProblems select").hide();
            $("#contests").val(0);
        }
    });
</script>
<% if (!Request.IsAuthenticated)
   {
%>
        <h2>Please Login First.</h2>
<%}
   else
   {
%>
    <h2>Submit Your Solution</h2>

    <%= Html.ValidationSummary("Error.")%>

    <% using (Html.BeginForm("Submit","Submit"))
       { %>
        <div>
            <fieldset>
                <legend>Submit a solution</legend>
                <p>
                            <% var contests = (IList<TPOJ.Model.Contest>)ViewData["CurrentContests"]; %>
                                <% if (contests.Count > 0) { %>
                                <% var selections = new List<SelectListItem>();
                                   selections.Add(new SelectListItem {Value = "0" });
                                       foreach (var item in contests) {
                                           var selectItem = new SelectListItem();
                                           selectItem.Text = item.Title;
                                           selectItem.Value = item.UniqueID.ToString();
                                           selections.Add(selectItem);
                                       }%>
                                       <%= Html.Encode("Participate in:") %>
                                       <%= Html.DropDownList("contests", selections)%>
                                <% } %>

                    <label for="problemId">Problem ID:</label>
                    <% if (contests.Count > 0) { %>
                    <span id="contestProblems">
                        <% foreach (var contest in contests) { %>
                            <%var selections = new List<SelectListItem>();
                              int i = -1;
                              foreach (var p in contest.Problems.OrderBy(p=>p.UniqueID))
                              {
                                  i++;
                                  var selectItem = new SelectListItem();
                                  selectItem.Text = Convert.ToChar('A' + i).ToString();
                                  selectItem.Value = p.UniqueID.ToString();
                                  selections.Add(selectItem);
                              }
                               %>
                               <%= Html.DropDownList(contest.UniqueID.ToString(), selections, new { onchange = "updateIdBox("+contest.UniqueID+")" })%>
                        <% } %>
                        </span>
                    <% } %>
                    <%= Html.TextBox("problemId")%>
                    <%= Html.ValidationMessage("problemId")%>
                    <br />
                    <label for="language">Language:</label>
                    <%= Html.DropDownList("language")%>
                    <br />
                    <label for="source">source:</label>
                    <%= Html.TextArea("source", new { rows = 20, cols = 100 })%>
                </p>
                <p>
                <input type="submit" value="Submit" /> <input type="reset" value="Reset" />
                </p>
            </fieldset>
        </div>
    <% }
   }%>
</asp:Content>
